<?php

namespace Modules\Custessa\Services;

use APP;
use CWidgetForm;
use CWidgetConfigMock;

eval (str_replace(
    ['<?php declare(strict_types = 1);', 'class CWidgetConfig {'],
    ['', 'namespace {class CWidgetConfigMock {'],
    file_get_contents(APP::getRootDir().'/include/classes/widgets/CWidgetConfig.php')
).'}');

class WidgetConfig extends CWidgetConfigMock {

    public static function getKnownWidgetTypes(string $context): array {
        $widgets = array_merge(parent::getKnownWidgetTypes($context), APP::Component()->widgets->list(
            function ($widget) use ($context) {
                return $widget->supportContext($context);
        }));
        asort($widgets);

        return $widgets;
    }

    public static function getForm($type, $data, $templateid): CWidgetForm {
        $registry = APP::Component()->widgets;
        
        if ($registry->has($type)) {
            $widget = $registry->$type;
            $widget->setFormContextArgumets([$data, $templateid]);

            return $widget->getForm();
        }

        return parent::getForm($type, $data, $templateid);
    }
}